(function (window) {
    function Player($audio) {
        return new Player.prototype.init($audio);
    }
    Player.prototype = {
        constructor:Player,
        musicList:[],
        init:function ($audio) {
            this.$audio = $audio;
            this.audio = $audio.get(0);
        },
        currentIndex:-1,
        playerMusic:function (index, music) {
            // 判断是同一首歌
            if(this.currentIndex == index){
                // 判断的状态，播放或者暂停
                if(this.audio.paused){
                    this.audio.play();
                }else{
                    this.audio.pause();
                }
            }else{
                this.$audio.attr("src",music.link_url);
                this.audio.play();
                this.currentIndex = index;
            }
        },
        preIndex:function () {

            var index = this.currentIndex -1;
            if(index <0){
                index = this.musicList.length - 1;
            }
            return index;
        },
        nextIndex:function () {

            var index = this.currentIndex + 1;

            if(index > this.musicList.length -1){
                console.log(index);
                index = 0;
                console.log(index);
            }
            return index;
        },

        changeMusic:function (index) {
            // 删掉数组中对应的数据
            this.musicList.splice(index,1);
            console.log(this.musicList.length);
            //判断当前删除的是否正在播放音乐的前面
            if(index < this.currentIndex){
                this.currentIndex = this.currentIndex -1;
            }
        },
        musicTimeUpdate:function (callBack) {
            var $this = this;
            this.$audio.on("timeupdate",function () {
                var duration = $this.audio.duration; //总时长
                var currentTime =  $this.audio.currentTime; //当前播放时长
                var timeStr = $this.formatDate(duration,currentTime);
                callBack(currentTime,duration,timeStr);
            })
        },
        //把时长的秒转为分钟
        formatDate: function (duration, currentTime) {
            var endMin = parseInt(duration / 60);
            var endSec = parseInt(duration % 60);
            if(endMin <10){
                endMin = "0" + endMin
            }
            if(endSec <10){
                endSec = "0" + endMin
            }
            //开始时间
            var startMin = parseInt(currentTime / 60);
            var startSec = parseInt(currentTime % 60);
            if(startMin <10){
                startMin = "0" + startMin
            }
            if(startSec <10){
                startSec = "0" + startSec
            }
            return startMin + ":" + startSec +" / " + endMin +":"+endSec;
        },
        musicSeekTo:function (value) {
            if(isNaN(value)) return;
            this.audio.currentTime = this.audio.duration * value;
        },
        musicVoiceSeekTo:function (value) {
            //0 到 1
            if(isNaN(value)) return;
            if(value<0 || value>1) return;
                this.audio.volume = value;
        }



    };
    Player.prototype.init.prototype = Player.prototype;
    window.Player = Player;
})(window);